package com.ruoyi.web.controller.activity.red;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.activity.domian.RedPacketLimitParameter;
import com.ruoyi.activity.service.RedPacketLimitParameterService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 红包限制+条件接口
 * @author chengjian 
 * @date 2021/9/9
 */
@RestController
@RequestMapping(value = "/activity/red/limit/parameter")
public class RedPacketLimitParameterController {

    @Resource
    private RedPacketLimitParameterService redPacketLimitParameterService;

    @PreAuthorize("@ss.hasPermi('activity:red:limit:parameter:list')")
    @GetMapping(value = "/list")
    public Page<RedPacketLimitParameter> list(Integer type, String beginTime, String endTime, int pageNum, int pageSize){
        return redPacketLimitParameterService.getList(type, beginTime, endTime, pageNum, pageSize);
    }

    @PreAuthorize("@ss.hasPermi('activity:red:limit:parameter:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getSingleConfig(@PathVariable Long id){

        return AjaxResult.success(redPacketLimitParameterService.getSingle(id));
    }

    @PreAuthorize("@ss.hasPermi('activity:red:limit:parameter:del')")
    @DeleteMapping(value = "/{ids}")
    public AjaxResult delConfig(@PathVariable Long[] ids){
        redPacketLimitParameterService.del(Arrays.stream(ids).collect(Collectors.toList()));
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('activity:red:limit:parameter:edit')")
    @PutMapping
    public AjaxResult updateConfig(@Validated @RequestBody RedPacketLimitParameter parameter){
        redPacketLimitParameterService.update(parameter);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('activity:red:limit:parameter:add')")
    @PostMapping
    public AjaxResult addConfig(@Validated @RequestBody RedPacketLimitParameter parameter){
        redPacketLimitParameterService.add(parameter);
        return AjaxResult.success();
    }

    @GetMapping(value = "/little/{id}")
    public AjaxResult getLittle(@PathVariable Long id){
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", redPacketLimitParameterService.getIdList(id));
        ajax.put("menus", redPacketLimitParameterService.getAll());
        return ajax;
    }

    @GetMapping(value = "/all")
    public AjaxResult getAll(){
        AjaxResult ajax = AjaxResult.success();
        ajax.put("menus", redPacketLimitParameterService.getAll());
        return ajax;
    }
}
